<?php
namespace Api\Service;

/**
 * 中国省市区
 */
class AreaService
{

    /**
     * 获取树类型
     * @return [type] [description]
     */
    public function detail()
    {
        $area = D('Area')->getAllTree();
        return $area;
    }

    /**
     * 获取所有省
     * @return [type] [description]
     */
    public function province()
    {
        $province = S('area_province');
        if (!$province) {
            $province = D('Area')->getProvince();
            S('area_province', $province);
        }
        return $province ? $province : [];
    }

    /**
     * 获取省下的城市
     * @param  [type] $province_id [description]
     * @return [type]              [description]
     */
    public function city($province_id)
    {
        if (!$province_id) {
            return [];
        }

        $city = S('area_city_' . $province_id);
        if (!$city) {
            $city = D('Area')->getCity($province_id);
            S('area_city_' . $province_id, $city);
        }
        return $city ? $city : [];
    }

    /**
     * 获取市的区县
     * @param  [type] $city_id [description]
     * @return [type]          [description]
     */
    public function region($city_id)
    {
        if (!$city_id) {
            return [];
        }

        $region = S('area_region_' . $city_id);
        if (!$region) {
            $region = D('Area')->getRegion($city_id);
            S('area_region_' . $city_id, $region);
        }
        return $region ? $region : [];
    }
}
